package com.quanyan.ic.video.task;

import com.quanyan.ic.middleware.lts.jobclient.JobClientFactoryBean;
import com.quanyan.ic.middleware.lts.jobclient.JobCompletedHandlerImpl;
import com.quanyan.ic.middleware.lts.jobclient.TaskTrackerAnnotationFactoryBean;
import com.quanyan.ic.video.constants.JobConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * Created by zhuo.shi on 2017/6/2.
 */
@Configuration
public class LtsConfiguration {

    @Value("${lts.zk.address}")
    private String registryAddress;

    private static Properties properties = new Properties();

    static {
        properties.setProperty("job.fail.store", "mapdb");
    }

    @Bean(initMethod = "start")
    public JobClientFactoryBean jobClient() {
        JobClientFactoryBean bean = new JobClientFactoryBean();
        bean.setClusterName(JobConstant.CLUSTER_NAME);
        bean.setRegistryAddress(registryAddress);
        bean.setNodeGroup(JobConstant.NODE_GROUP_CLIENT);
        bean.setJobCompletedHandler(new JobCompletedHandlerImpl());
        bean.setConfigs(properties);
        return bean;
    }

    @Bean(initMethod = "start")
    public TaskTrackerAnnotationFactoryBean taskTracker() {
        TaskTrackerAnnotationFactoryBean bean = new TaskTrackerAnnotationFactoryBean();
        bean.setClusterName(JobConstant.CLUSTER_NAME);
        bean.setJobRunnerClass(com.quanyan.ic.video.task.VideoJobRunnerDispatcher.class);
        bean.setRegistryAddress(registryAddress);
        bean.setNodeGroup(JobConstant.NODE_GROUP_TRACKER);
        bean.setWorkThreads(64);
        bean.setConfigs(properties);
        return bean;
    }

    @Bean(initMethod = "initTask")
    public TimerTaskJob timerTaskJob() {
        return new TimerTaskJob();
    }


}
